
package equipment_management

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipment_management"
    equipment_managementReq "github.com/flipped-aurora/gin-vue-admin/server/model/equipment_management/request"
)

type AllVersionsService struct {}
// CreateAllVersions 创建版本详情记录
// Author [yourname](https://github.com/yourname)
func (AVService *AllVersionsService) CreateAllVersions(ctx context.Context, AV *equipment_management.AllVersions) (err error) {
	err = global.GVA_DB.Create(AV).Error
	return err
}

// DeleteAllVersions 删除版本详情记录
// Author [yourname](https://github.com/yourname)
func (AVService *AllVersionsService)DeleteAllVersions(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&equipment_management.AllVersions{},"id = ?",ID).Error
	return err
}

// DeleteAllVersionsByIds 批量删除版本详情记录
// Author [yourname](https://github.com/yourname)
func (AVService *AllVersionsService)DeleteAllVersionsByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]equipment_management.AllVersions{},"id in ?",IDs).Error
	return err
}

// UpdateAllVersions 更新版本详情记录
// Author [yourname](https://github.com/yourname)
func (AVService *AllVersionsService)UpdateAllVersions(ctx context.Context, AV equipment_management.AllVersions) (err error) {
	err = global.GVA_DB.Model(&equipment_management.AllVersions{}).Where("id = ?",AV.ID).Updates(&AV).Error
	return err
}

// GetAllVersions 根据ID获取版本详情记录
// Author [yourname](https://github.com/yourname)
func (AVService *AllVersionsService)GetAllVersions(ctx context.Context, ID string) (AV equipment_management.AllVersions, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&AV).Error
	return
}
// GetAllVersionsInfoList 分页获取版本详情记录
// Author [yourname](https://github.com/yourname)
func (AVService *AllVersionsService)GetAllVersionsInfoList(ctx context.Context, info equipment_managementReq.AllVersionsSearch) (list []equipment_management.AllVersions, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&equipment_management.AllVersions{})
    var AVs []equipment_management.AllVersions
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.EquipmentModel != nil && *info.EquipmentModel != "" {
        db = db.Where("equipment_model LIKE ?", "%"+ *info.EquipmentModel+"%")
    }
    if info.Version != nil && *info.Version != "" {
        db = db.Where("version = ?", *info.Version)
    }
    if info.ApplicationSoftware != nil && *info.ApplicationSoftware != "" {
        db = db.Where("application_software = ?", *info.ApplicationSoftware)
    }
    if info.UpgradeRequirements != nil && *info.UpgradeRequirements != "" {
        db = db.Where("upgrade_requirements = ?", *info.UpgradeRequirements)
    }
    if info.ReleaseNote != nil && *info.ReleaseNote != "" {
        db = db.Where("release_note LIKE ?", "%"+ *info.ReleaseNote+"%")
    }
    if info.EquipmentName != nil && *info.EquipmentName != "" {
        db = db.Where("equipment_name LIKE ?", "%"+ *info.EquipmentName+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&AVs).Error
	return  AVs, total, err
}
func (AVService *AllVersionsService)GetAllVersionsPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
